/**
 * Copyright (c) 2013 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.gravidroid.paint;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.view.View;
import chabernac.android.draw.AbstractDrawable;
import chabernac.gravidroid.model.Mass;

public class MassDrawable extends AbstractDrawable {
	private final Mass myMass;
	private final Paint myPaint = new Paint();
	private final Rect myDrawingRect = new Rect();
	private final int BORDER = 300;

	private int myFirstColor = Color.WHITE;
	private int myEndingColor = Color.BLACK;

	private float myForceRadius = 0;
	private float myForceRadiusStep = 5F; 

	public MassDrawable( Mass aMass ) {
		super();
		myMass = aMass;

		myPaint.setColor( Color.RED );
		myPaint.setStyle( Style.FILL );
		myForceRadius = myMass.coreRadius;
	}

	@Override
	public void paint( Canvas aCanvas, View aView ) {
		//    System.out.println("Drawing mass at : " + myMass.location);

		if(myMass.hasForceField){
			myPaint.setStyle( Style.FILL );

			RadialGradient theGradient = new RadialGradient(myMass.location.x, myMass.location.y, myForceRadius, myFirstColor, myEndingColor, Shader.TileMode.CLAMP);
			myPaint.setShader(theGradient);
			aCanvas.drawCircle( myMass.location.x, myMass.location.y, myMass.radius, myPaint );

			//    	theGradient = new RadialGradient(myMass.location.x, myMass.location.y, myForceRadius, myFirstColor, Color.GRAY, Shader.TileMode.CLAMP);
			//    	myPaint.setShader(theGradient);
			//    	aCanvas.drawCircle( myMass.location.x, myMass.location.y, myForceRadius, myPaint );

			myForceRadius += myForceRadiusStep;
			if(myForceRadius > myMass.radius){
				//      	myForceRadius = myMass.coreRadius;
				myForceRadiusStep = -20;
			}
			if(myForceRadius < myMass.coreRadius * 2){
				myForceRadiusStep = 4;
			}
			myPaint.setShader(null);
		}

		myPaint.setColor( myMass.color );
		myPaint.setStyle( Style.FILL );
		aCanvas.drawCircle( myMass.location.x, myMass.location.y, myMass.coreRadius, myPaint );

		if(!myMass.hasForceField){
			myPaint.setColor(myMass.borderColor);
			myPaint.setStyle(Style.STROKE);
			aCanvas.drawCircle( myMass.location.x, myMass.location.y, myMass.coreRadius, myPaint );
		}
		
//		myPaint.setColor(Color.WHITE );
//		aCanvas.drawLine(myMass.location.x, myMass.location.y, myMass.location.x + myMass.force.x, myMass.location.y + myMass.force.y, myPaint);

		aView.getDrawingRect(myDrawingRect);

//		if(myMass.location.x < myDrawingRect.left + BORDER){
//			//invert the x component of the speed
//			if(myMass.speed.x < 0) myMass.speed.invertX();
//		} else if(myMass.location.x > myDrawingRect.right - BORDER){
//			if(myMass.speed.x > 0) myMass.speed.invertX();
//		}
//
//		if(myMass.location.y < myDrawingRect.top + BORDER){
//			//invert the y component of the speed
//			if(myMass.speed.y < 0) myMass.speed.invertY();
//		} else if(myMass.location.y > myDrawingRect.bottom - BORDER){
//			if(myMass.speed.y > 0) myMass.speed.invertY();
//		}
	}

	public int getLayer(){
		return (int)myMass.radius;
	}

}
